<?php defined('SYSPATH') or die('No direct script access.');

abstract class Controller_Common extends Controller_Template {

	public function before() {
        parent::before();
        View::set_global('title', 'Туры');
        View::set_global('user', ORM::factory('users', array('email' => Cookie::get('user', null))));

        if (!empty($_COOKIE['myRegionCountry'])) {
            $cookie = explode('!', $_COOKIE['myRegionCountry']);
            $myRegionCountry = $cookie[0];
        }
        else {
            $myRegionCountry = 'Выбрать';
        }

        if (!empty($_COOKIE['myRegionCity'])) {
            $cookie = explode('!', $_COOKIE['myRegionCity']);
            $myRegionCountry = $cookie[0];
        }

        View::set_global('myRegionCountry', $myRegionCountry);
        //View::set_global('siteText', Model::factory('functions')->getSiteText());
	}

    public static function buildCatalogTree($categories, $parentId, $display = 'none') {
    	$tree='';

        if (isset($categories[$parentId])) {
            foreach ($categories[$parentId] as $item) {
            	$childrens = self::buildCatalogTree($categories, $item['id']);

            	if ($childrens != '') {
            		$expand = '<img src="'.URL::site('static/css/plus.png').'" alt="plus" class="expand" />';
            	}
            	else {
            		$expand = '';
            	}

                $tree .= '
                	<div class="item" style="display: '.$display.'">
                		'.$expand.'
                        <a href="#" onclick="loadContent(\''.URL::site('place/'.$item['id']).'\')">'.$item['title'].'</a>
                		'.self::buildCatalogTree($categories, $item['id']).'
                	</div>
                ';
            }
		}

        return $tree;
    }

    public function buildCarcas() {
        $this->template->header=View::factory('pieces/header');

        $allCat = Model::factory('places')->getAll();

        /*foreach ($allCat as $item) {
            $allCatalog[$item['parent_id']][] = $item;
        }*/

        $menu = Model::factory('menu')->getMenu();

        $this->template->leftContent=View::factory('pieces/left_content', array(
            //'catalog' => $this->buildCatalogTree($allCatalog, 0, 'block'),
            'catalog' => Model::factory('places')->get_countries(),
            'menu' => $menu
        ));

        $this->template->footer=View::factory('pieces/footer', array(
            'menu' => $menu
        ));
    }

}
